<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Display line that crosses 180th meridian</title>
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <script src="../../lib/mapbox-gl.js"></script>
    <link href="../../lib/mapbox-gl.css" rel="stylesheet" />
    <script src="../../lib/axios.min.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      let baseDataUrl = `http://${localStorage.getItem(
        "host"
      )}/mapbox-gl-js-offline-examples`;
      axios
        .get(
          baseDataUrl +
            `/resources/styles/${localStorage.getItem(
              "mbtilesStylesName"
            )}.json`
        )
        .then((res) => {
          let data = res.data;
          initMap(data);
        })
        .catch((err) => {
          console.error(err);
        });

      function initMap(style) {
        let map = new mapboxgl.Map({
          container: "map",
          center: [-41.62667, 26.11598],
          zoom: 0,
          style: style,
        });

        map.on("load", function () {
          map.addLayer({
            id: "route",
            type: "line",
            source: {
              type: "geojson",
              data: {
                type: "Feature",
                properties: {},
                geometry: createGeometry(false),
              },
            },
            layout: { "line-cap": "round" },
            paint: {
              "line-color": "#007296",
              "line-width": 4,
            },
          });

          map.addLayer({
            id: "route-label",
            type: "symbol",
            source: "route",
            layout: {
              "symbol-placement": "line-center",
              "text-field": "Crosses the world",
              "text-font": ["Open Sans Regular"],
            },
          });

          map.addLayer({
            id: "route-two",
            type: "line",
            source: {
              type: "geojson",
              data: {
                type: "Feature",
                properties: {},
                geometry: createGeometry(true),
              },
            },
            layout: { "line-cap": "round" },
            paint: {
              "line-color": "#F06317",
              "line-width": 4,
            },
          });

          map.addLayer({
            id: "route-two-label",
            type: "symbol",
            source: "route-two",
            layout: {
              "symbol-placement": "line-center",
              "text-field": "Crosses 180th meridian",
              "text-font": ["Open Sans Regular"],
            },
          });

          function createGeometry(doesCrossAntimeridian) {
            var geometry = {
              type: "LineString",
              coordinates: [
                [-72.42187, -16.59408],
                [140.27343, 35.67514],
              ],
            };

            // To draw a line across the 180th meridian,
            // if the longitude of the second point minus
            // the longitude of original (or previous) point is >= 180,
            // subtract 360 from the longitude of the second point.
            // If it is less than 180, add 360 to the second point.

            if (doesCrossAntimeridian) {
              var startLng = geometry.coordinates[0][0];
              var endLng = geometry.coordinates[1][0];

              if (endLng - startLng >= 180) {
                geometry.coordinates[1][0] -= 360;
              } else if (endLng - startLng < 180) {
                geometry.coordinates[1][0] += 360;
              }
            }

            return geometry;
          }
        });
      }
    </script>
  </body>
</html>
